import 'package:boss/models/commodity.dart';
import 'package:boss/models/package_group.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/packageGroup/page/packageGroupDetail/provider/package_group_detail_provider.dart';
import 'package:boss/ui/widget/custom_list_title.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:boss/ui/widget/picker/commodityPicker/commodity_picker.dart';
import 'package:boss/ui/widget/radioList/model/radio_item_data.dart';
import 'package:boss/ui/widget/radioList/radio_list_picker.dart';
import 'package:boss/ui/widget/sortList/sort_item_data.dart';
import 'package:boss/ui/widget/sortList/sort_util.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'commodity_row.dart';

class PackageGroupDetailCommoditys extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        CustomListTitle(
          title: RichText(
            text: TextSpan(text: '分组包含菜品', style: TextStyleConstant().normal_1_28, children: [
              TextSpan(
                text: '*',
                style: TextStyleConstant().origin_28,
              )
            ]),
          ),
          trailing: LabelButton(
            textStyle: TextStyleConstant().blue_28,
            onTap: () async {
              var provider = Provider.of<PackageGroupDetailProvider>(
                context,
                listen: false,
              );

              var selected = provider.packageGroup.commodityFormList
                  .map((item) => Commodity.fromJson({
                        'id': item.commodityId,
                        'sku': {
                          'skuId': item.skuId,
                        }
                      }))
                  .toList();

              var commoditys = await showCommodityPicker(
                context,
                selectedCommodities: selected,
                isShowPackage: false,
                isShowSku: true,
              );

              if (commoditys != null) {
                provider.setPackageGroupCommoditys(commoditys);
              }
            },
            title: '请选择菜品',
          ),
        ),
        Consumer<PackageGroupDetailProvider>(
          builder: (context, provider, _) {
            if (provider.packageGroup.commodityFormList.length > 0) {
              return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  SizedBox(
                    height: 70.w,
                    child: Row(
                      children: <Widget>[
                        Expanded(
                          flex: 2,
                          child: Text(
                            '菜品',
                            style: TextStyleConstant().normal_3_28,
                          ),
                        ),
                        Expanded(
                          child: Text(
                            '加价',
                            style: TextStyleConstant().normal_3_28,
                          ),
                        ),
                      ],
                    ),
                  ),
                  ...provider.packageGroup.commodityFormList.map((item) => CommodityRow(
                        key: item.key,
                        data: item,
                      )),
                  Container(
                    height: 70.w,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: <Widget>[
                        LabelButton(
                          title: '设置默认项',
                          textStyle: TextStyleConstant().blue_28,
                          onTap: () async {
                            var res = await showRadioListPicker<CommodityFormList>(
                              context,
                              title: '设置默认项',
                              items: provider.packageGroup.commodityFormList
                                  .map((item) => RadioItemData<CommodityFormList>(title: item.commodityNameWithSku, value: item))
                                  .toList(),
                              value: provider.defaultedCommodityFormList,
                              isMultiple: true,
                            );
                            if (res != null) {
                              if (res.length > provider.packageGroup.count) {
                                toast('默认项不能超过菜品必点数量');
                                return;
                              }

                              provider.setPackageGroupCommodityDefaulted(res);
                            }
                          },
                        ),
                        SizedBox(
                          width: 20.w,
                        ),
                        LabelButton(
                          title: '调整排序',
                          textStyle: TextStyleConstant().blue_28,
                          onTap: () async {
                            var res = await showSortList<CommodityFormList>(
                              context,
                              title: '套餐菜品排序',
                              items:
                                  provider.packageGroup.commodityFormList.map((item) => SortItemData(title: item.commodityNameWithSku, value: item)).toList(),
                            );
                            if (res != null) {
                              provider.sortPackageGroupCommoditys(res);
                            }
                          },
                        ),
                      ],
                    ),
                  ),
                ],
              );
            }

            return SizedBox();
          },
        ),
      ],
    );
  }
}
